﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Way.Bus.WMS.Controllers;
using Way.Bus.WMS.SF.Model;
using Way.Bus.WMS.Model;
using Way.EAP.DataAccess.Entity;
using Way.Contract.MVC;
using System.Data;
using System.Configuration;
using InterfaceService.DataAccess;
using Way.Contract.Data;

namespace Way.Bus.WMS.SF.Controllers
{
    public class SFMaterialsController : MaterialsController
    {
        public SFMaterialsController(): base(new SFMaterials())
        {
        }

        //显示在菜单
        protected override string OnControllerName()
        {
            return "产品信息管理";
        }
        //菜单中url路径
        protected override string OnGetPath()
        {
            return "Views/SF/SFMaterials.htm";
        }
        //开发人
        protected override string OnGetAuthor()
        {
            return "路聪";
        }
        //说明
        protected override string OnGetControllerDescription()
        {
            return "产品信息管理";
        }



        #region 初始化数据方法
        protected override void OnInitViewChildItem(Contract.Data.ModelData data, Contract.Data.ItemData item)
        {
            base.OnInitViewChildItem(data, item);
            if (data.name.EndsWith("Materials"))
            {

                switch (item.field.ToLower())
                {
                    case "code":
                    case "fname":
                    case "funitid":
                    case "netweight":
                    case "zmm":
                    case "storeamount":
                    case "remark":
                    case "fmodel":
                        item.visible = true;
                        break;
                    default:
                        item.visible = false;
                        break;
                }
            }
            if (data.name.EndsWith("MaterialUnit"))
            {
                data.visible = false;
                switch (item.field.ToLower())
                {
                    case "unitid":
                        item.title = "单位";
                        break;
                }
            }
            if (data.name.EndsWith("PackUnitList"))
            {
                data.visible = false;
                data.title = "商品BOM";
            }
        }

        #endregion


        protected override ActionCommand[] OnInitCommand(ActionCommand[] commands)
        {
            //获取所有按钮集合
            foreach (ActionCommand ac in commands)
            {
                if (ac.name == "删除" || ac.name == "提交" || ac.name == "审核" || ac.name == "禁用" || ac.name == "导入" || ac.command == "禁用")
                {
                    ac.visible = false;
                }
            }
            return commands;
        }

        protected override void OnAdding(Contract.MVC.ControllerBase.SaveEvent item)
        {
            base.OnAdding(item);
            SFMaterials material = item.Item as SFMaterials;
            ValidateInfo(material);
        }


        protected override void OnEditing(Contract.MVC.ControllerBase.SaveEvent item)
        {
            base.OnEditing(item);
            SFMaterials material = item.Item as SFMaterials;
            ValidateInfo(material);
            InsertOrUpdateQyk(material);
        }
        /// <summary>
        /// 验证输入的信息是否可行
        /// </summary>
        /// <param name="order"></param>
        private void ValidateInfo(Materials material)
        {
            EntityList<Materials> list = new EntityList<Materials>(this.model.GetDataAction());
            EntityList<Materials> list1 = new EntityList<Materials>(this.model.GetDataAction());
            EntityList<Materials> list2 = new EntityList<Materials>(this.model.GetDataAction());
            EntityList<Materials> list3 = new EntityList<Materials>(this.model.GetDataAction());
            if (material.Code.Length != 9)
                throw new Exception("请正确输入产品编码！");
            //说明是修改
            if (material.ID != 0)
            {
                list.GetData("Code = '" + material.Code + "' and id<>" + material.ID + "");
                list1.GetData("FNAME ='" + material.FNAME + "' and id<>" + material.ID + "");
                list2.GetData("FMODEL ='" + material.FMODEL + "' and id<>" + material.ID + "");
                list3.GetData("FNAME ='" + material.FNAME + "' and FMODEL ='" + material.FMODEL + "' and id<>" + material.ID + "");
            }
            else
            {
                list.GetData("Code = '" + material.Code + "'");
                list1.GetData("FNAME ='" + material.FNAME + "'");
                list2.GetData("FMODEL ='" + material.FMODEL + "'");
                list3.GetData("FNAME ='" + material.FNAME + "' and FMODEL ='" + material.FMODEL + "'");
            }
            if (list3.Count > 0)
            {
                throw new Exception("产品商品名称和规格不可重复");
            }
            if (list.Count > 0)
            {
                throw new Exception("产品编码不可重复");
            }
            else if (material.NETWEIGHT < 0)
            {
                throw new Exception("请输入正确的产品净重量");
            }
            else if (material.PRICE < 0)
            {
                throw new Exception("请输入正确的产品价格");
            }
            else if (material.LOWESTSTOCK < 0)
            {
                throw new Exception("请输入正确的产品最低库存量");
            }
            else
            {

            }
        }
        protected override void OnAdded(EntityBase item)
        {
            base.OnAdded(item);
            SFMaterials ms = item as SFMaterials;
            InsertOrUpdateQyk(ms);
        }

        protected override void OnRemoveing(Contract.MVC.ControllerBase.SaveEvent item)
        {
            SFMaterials reg = item.Item as SFMaterials;
            DeleteQyk(reg);
            base.OnRemoveing(item);
        }

        private void InsertOrUpdateQyk(SFMaterials reg)
        {
            reg.FNAME = reg.FNAME.Trim();
            string strSql = "SELECT * from Kmh where Bm='" + reg.Code + "'";
            string strCon = ConfigurationManager.AppSettings["FOXCWVW"].ToString() + "kmh.DBF" + ConfigurationManager.AppSettings["ENDCONN"].ToString();
            DataTable dt = BusFoxProHelper.ExecuteDataTable(strCon, CommandType.Text, strSql);
            if (dt.Rows.Count == 0)
            {//reg.ZMM
                strSql = "insert into Kmh(Bm,Zmm,Kmm,Mxbz,Zbbm,Tjbl,Slbz,Bz,Yifl,Erfl,Safl,Sifl) values('" + reg.Code + "','" + reg.ZMM + "','" + reg.FNAME + "','','',0,'','','','','','')";
                strCon = ConfigurationManager.AppSettings["FOXCWVW"].ToString() + "kmh.DBF" + ConfigurationManager.AppSettings["ENDCONN"].ToString();
                BusFoxProHelper.ExecuteNonQuery(strCon, CommandType.Text, strSql);
                strSql = "insert into Kmh(Bm,Zmm,Kmm,nbjg,til,qdsl,xx,ux,tuf,mxbz,slfl,bvl1,bvu1,bvl2,bvu2) values('" + reg.Code.ToString().Substring(3, 6) + "','" + reg.ZMM + "','" + reg.FNAME + "',0,0,0,0,0,'','','','',0,'',0)";
                strCon = ConfigurationManager.AppSettings["FOXIPKC"].ToString() + "kmh.DBF" + ConfigurationManager.AppSettings["ENDCONN"].ToString();
                BusFoxProHelper.ExecuteNonQuery(strCon, CommandType.Text, strSql);
                strSql = "insert into Kmh(Bm,Zmm,Kmm,mxbz,Nbjg,Til,Qdsl,slfl,Zbbm,Tjbl) values('" + reg.Code.ToString().Substring(3, 6) + "','" + reg.ZMM + "','" + reg.FNAME.ToString() + "','',0,0,0,'','',0)";
                strCon = ConfigurationManager.AppSettings["FOXXG"].ToString() + "kmh.DBF" + ConfigurationManager.AppSettings["ENDCONN"].ToString();
                BusFoxProHelper.ExecuteNonQuery(strCon, CommandType.Text, strSql);
                if (reg.Code.Substring(0, 3) == "137")
                {
                    strSql = "insert into Kmh(Bm,Zmm,Kmm,Mxbz,Zbbm,Tjbl,Slbz,Bz,Yifl,Erfl,Safl,Sifl) values('501" + reg.Code.Substring(3, 6) + "','" + reg.ZMM + "','" + reg.FNAME + "','','',0,'','','','','','')";
                    strCon = ConfigurationManager.AppSettings["FOXCWVW"].ToString() + "kmh.DBF" + ConfigurationManager.AppSettings["ENDCONN"].ToString();
                    BusFoxProHelper.ExecuteNonQuery(strCon, CommandType.Text, strSql);
                    strSql = "insert into Kmh(Bm,Zmm,Kmm,Mxbz,Zbbm,Tjbl,Slbz,Bz,Yifl,Erfl,Safl,Sifl) values('502" + reg.Code.Substring(3, 6) + "','" + reg.ZMM + "','" + reg.FNAME + "','','',0,'','','','','','')";
                    strCon = ConfigurationManager.AppSettings["FOXCWVW"].ToString() + "kmh.DBF" + ConfigurationManager.AppSettings["ENDCONN"].ToString();
                    BusFoxProHelper.ExecuteNonQuery(strCon, CommandType.Text, strSql);
                }
            }
            else
            {
                strSql = "update kmh set Kmm='" + reg.FNAME + "' , Zmm='" + reg.ZMM + "' where Bm='" + reg.Code + "'";
                strCon = ConfigurationManager.AppSettings["FOXCWVW"].ToString() + "kmh.DBF" + ConfigurationManager.AppSettings["ENDCONN"].ToString();
                BusFoxProHelper.ExecuteNonQuery(strCon, CommandType.Text, strSql);
                strSql = "update kmh set Kmm='" + reg.FNAME + "' , Zmm='" + reg.ZMM + "' where Bm='501" + reg.Code.Substring(3, 6) + "'";
                BusFoxProHelper.ExecuteNonQuery(strCon, CommandType.Text, strSql);
                strSql = "update kmh set Kmm='" + reg.FNAME + "' , Zmm='" + reg.ZMM + "' where Bm='502" + reg.Code.Substring(3, 6) + "'";
                BusFoxProHelper.ExecuteNonQuery(strCon, CommandType.Text, strSql);
                strSql = "update kmh set Kmm='" + reg.FNAME + "'  , Zmm='" + reg.ZMM + "'  where Bm='" + reg.Code.Substring(3, 6) + "'";
                strCon = ConfigurationManager.AppSettings["FOXIPKC"].ToString() + "kmh.DBF" + ConfigurationManager.AppSettings["ENDCONN"].ToString();
                BusFoxProHelper.ExecuteNonQuery(strCon, CommandType.Text, strSql);
                strSql = "update kmh set Kmm='" + reg.FNAME + "' , Zmm='" + reg.ZMM + "'  where  Bm='" + reg.Code.Substring(3, 6) + "'";
                strCon = ConfigurationManager.AppSettings["FOXXG"].ToString() + "kmh.DBF" + ConfigurationManager.AppSettings["ENDCONN"].ToString();
                BusFoxProHelper.ExecuteNonQuery(strCon, CommandType.Text, strSql);
            }
        }

        private void DeleteQyk(SFMaterials reg)
        {
            string strSql = "update kmh set Kmm='' , Zmm='',Bm='' where Bm='" + reg.Code + "'";
            string strCon = ConfigurationManager.AppSettings["FOXCWVW"].ToString() + "kmh.DBF" + ConfigurationManager.AppSettings["ENDCONN"].ToString();
            BusFoxProHelper.ExecuteNonQuery(strCon, CommandType.Text, strSql);
            strSql = "update kmh set Kmm=''  , Zmm='',Bm=''  where Bm='" + reg.Code.Substring(3, 6) + "'";
            strCon = ConfigurationManager.AppSettings["FOXIPKC"].ToString() + "kmh.DBF" + ConfigurationManager.AppSettings["ENDCONN"].ToString();
            BusFoxProHelper.ExecuteNonQuery(strCon, CommandType.Text, strSql);
            strSql = "update kmh set Kmm='' , Zmm='',Bm=''  where  Bm='" + reg.Code + "'";
            strCon = ConfigurationManager.AppSettings["FOXXG"].ToString() + "kmh.DBF" + ConfigurationManager.AppSettings["ENDCONN"].ToString();
            BusFoxProHelper.ExecuteNonQuery(strCon, CommandType.Text, strSql);
            if (reg.Code.Substring(0, 3) == "137")
            {
                strSql = "update kmh set Kmm='' , Zmm='',Bm='' where Bm='501" + reg.Code.Substring(3, 6) + "'";
                strCon = ConfigurationManager.AppSettings["FOXCWVW"].ToString() + "kmh.DBF" + ConfigurationManager.AppSettings["ENDCONN"].ToString();
                BusFoxProHelper.ExecuteNonQuery(strCon, CommandType.Text, strSql);
                strSql = "update kmh set Kmm='' , Zmm='',Bm='' where Bm='502" + reg.Code.Substring(3, 6) + "'";
                strCon = ConfigurationManager.AppSettings["FOXCWVW"].ToString() + "kmh.DBF" + ConfigurationManager.AppSettings["ENDCONN"].ToString();
                BusFoxProHelper.ExecuteNonQuery(strCon, CommandType.Text, strSql);
            }
        }

    }
}
